<template>
  <div class="text-message" :class="{ left: float == 'left', right: float == 'right', 'max-width': !fullWidth}">
    <div v-if="arrow" class="arrow"></div>
    <pre v-html="html" />
  </div>
</template>
<script>
import { textReplaceLink } from '@/utils/functions'
import { textReplaceEmoji } from '@/utils/emojis'

export default {
  name: 'TextMessage',
  props: {
    content: {
      type: [String, Number],
      default: ''
    },
    float: {
      type: String,
      default: 'left'
    },
    fullWidth: {
      type: Boolean,
      default: true
    },
    arrow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      html: ''
    }
  },
  created() {
    const text = textReplaceLink(
      this.content,
      this.float == 'right' ? '#ffffff' : 'rgb(9 149 208)'
    )
    this.html = textReplaceEmoji(text)
  }
}
</script>
<style lang="scss" scoped>
.text-message {
  position: relative;
  min-width: 20px;
  min-height: 10px;
  border-radius: 5px;
  padding: 0px;
  .arrow {
    position: absolute;
    width: 0;
    height: 0;
    font-size: 0;
    border: 5px solid;
    top: 6px;
    left: -10px;
  }

  &.max-width {
    max-width: calc(100% - 100px);
  }
  &.left {
    color: #3a3a3a;
    background:#f5f5f5;
    .arrow {
      border-color: transparent #f5f5f5 transparent transparent;
    }
  }
  &.right {
    color: #000000;
    background: #d2ecf8;
    .arrow {
      right: -10px;
      left: unset;
      border-color: transparent transparent transparent #d2ecf8;
    }
  }
  pre {
    white-space: pre-wrap;
    overflow: hidden;
    word-break: break-word;
    word-wrap: break-word;
    font-size: 12px;
    padding: 0px 10px 0px 10px;
    font-family: 'Microsoft YaHei';
    line-height: 20px;
  }
}
</style>
